// miniprogram/pages/journal/journal.js
//获取应用实例
const app = getApp();
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    passIndex: 0,
    gold: 20,
    selected: ['', '', '', ''],
    answer: '',
    error: '',
    subject: undefined,
    title: '秀才',
    successMsg: '恭喜已通关',
    dialogSuccessShow: false,
    restartBtn: [{
      text: '重新开始'
    }],
    dialogErrorShow: false,
    confirmBtn: [{
      text: '确定'
    }],
    selectList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.list)
    this.setPassInfo();
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  setPassInfo: function () {
    /* 初始化默认已选择列表 */

    var urlContent = app.globalData.jokeUrl + '/api/subject/list';
    var params = {
      current: this.data.current,
      size: 10
    };

    var token = app.globalData.token;
    var that = this;
    //@todo 文章内容网络请求API数据
    wx.request({
      url: urlContent,
      method: "GET",
      // data: params,
      header: {
        'joke_token': token
      },
      success: function (res) {
        if (res.data && res.data.code == 200) {
          console.log(res.data.data)
          var list = res.data.data;
          that.setData({
            list: list,
            show: false,
            subject: list[0],
            imageUrl: list[0].title
          });
        }
      },
      fail: function (res) {
        console.error(res)
      }
    });
  },

  useHelp: function () {
    let gold = this.data.gold;
    var subject = this.data.subject;
    var tip = subject.answer[0];
    let temp = [tip,'','',''];
    this.setData({
      selected: temp
    });
  },

  fillAnswer: function (e) {
    var content = e.detail.value.replace(/\s+/g, '');
    this.setData({
      answer: content
    });
  },

  picker: function (e) {
    let index = e.currentTarget.dataset.index;

    let character = this.data.subject.characterSet[index];
    if (character) {
      for (var i = 0; i < 4; i++) {
        if (!this.data.selected[i]) {
          let temp = this.data.selected;
          temp[i] = character;
          this.setData({
            selected: temp
          })
          if (i === 3) {
            this.verify();
          }
          break;
        }
      }
    }
  },

  resetPicker: function(){
    this.setData({
      selected: ['', '', '', '']
    });
  },

  cancelPicker:function(e){
    let index = e.currentTarget.dataset.index;
    let temp = this.data.selected;
    
    temp[index] = '';
    console.log(e, index, temp);
    this.setData({
      selected: temp
    });

  },

  verify: function () {
    let userAnswer = this.data.selected.join('');
    let answer = this.data.subject.answer;
    console.log('userAnswer:', userAnswer, 'answer:', answer)
    if (userAnswer === answer) {
      this.setData({
        gold: this.data.gold + 5
      });
      this.nextPass();
    } else {
      this.setData({
        dialogErrorShow: true,
        successMsg: '答案不对哦'
      });
    }
  },

  submit: function () {

    if (this.data.answer === this.data.subject.answer) {
      this.gold += 5;
      this.nextPass();

    } else {
      this.setData({
        error: '答案不对哦'
      })
    }
  },

  nextPass: function () {
    if ((this.data.list.length - 1) <= this.data.passIndex) {
      this.setData({
        dialogSuccessShow: true
      });
    } else {
      var index = this.data.passIndex;
      index++;
      var subject = this.data.list[index]
      console.log(subject)
      this.setData({
        passIndex: index,
        subject: subject,
        answer: '',
        selected: ['', '', '', '']
      });
    }
  },

  tapDialogButton: function () {
    this.setData({
      dialogSuccessShow: false,
      passIndex: 0,
      answer: ''
    });
    this.setPassInfo();
  },

  tapConfirmBtn:function(){
    this.setData({
      dialogErrorShow: false
    });
  },

  share:function(){
    
  }
})